package com.zqxq.lottery.controller.product;

import com.baomidou.mybatisplus.plugins.Page;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.lottery.entity.bo.product.ProductBO;
import com.zqxq.lottery.entity.bo.product.ProductSearchBO;
import com.zqxq.lottery.entity.o2o.product.ProductO2O;
import com.zqxq.lottery.entity.po.product.ProductPO;
import com.zqxq.lottery.entity.vo.product.ProductVO;
import com.zqxq.lottery.entity.vo.user.UserProductVO;
import com.zqxq.lottery.service.product.IProductService;
import com.zqxq.lottery.service.user.IUserProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IUserProductService userProductService;
    @PostMapping("/save")
    public Result save(@RequestBody ProductVO vo){
        Result rs = new Result();
        try{
            ProductVO order = productService.save(vo);
            rs.setData(order);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("Product Save  Error:",e);
        }
        return rs;
    }
    @PostMapping("/page")
    public Result page(@RequestBody ProductBO search){
        if(search.getStatus()==null){
            search.setStatus(0);
        }
        Result rs = new Result();
        try{
            Page page = productService.queryPage(search);
            rs.setData(page);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("Product Page  Error:",e);
        }
        return rs;
    }

    @PostMapping("/get")
    public Result page(@RequestParam("productId") Long productId){
        Result rs = new Result();
        try{
            ProductPO po = productService.selectById(productId);
            rs.setData(ProductO2O.toVO(po));
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("Product Get  Error:",e);
        }
        return rs;
    }

    @PostMapping("/excelBatchInsert")
    public Result excelBatchInsert(@RequestParam("fileName") String fileName,@RequestBody Map<String, File> fileMap,@RequestParam("isUpdate") boolean isUpdate) {
        Result rs = new Result();
        File file = fileMap.get("image_ref1");
        FileInputStream input = null;
        try {
            input = new FileInputStream(file);
            MultipartFile multipartFile = new MockMultipartFile(fileName, fileName, "text/plain", IOUtils.toByteArray(input));
            boolean flag = productService.excelBatchInsert(fileName, multipartFile,isUpdate);
            rs.setData(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }
    @PostMapping("/excelBatchExport")
    public Result<List<ProductVO>> excelBatchExport(@RequestBody ProductSearchBO search){
        Result<List<ProductVO>> rs = new Result();
        try{
            List<ProductVO> list = productService.query(search);
            rs.setData(list);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("Product Query  Error:",e);
        }
        return rs;
    }

    @PostMapping("/payDataProcure")
    public Result payDataProcure(@RequestParam("userId") Long userId,@RequestParam("productId") Long productId){
        Result rs = new Result();
        try{
            Map<String,Object> map = productService.payDataProcure(userId,productId);
            rs.setData(map);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("payDataProcure Get  Error:",e);
        }
        return rs;
    }

    @PostMapping("/addProductOrder")
    public Result addProductOrder(HttpServletRequest request, @RequestBody UserProductVO vo){
        Result rs = new Result();
        try{
            rs = userProductService.save(request,vo);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("addProductOrder Error:",e);
        }
        return rs;
    }
}
